package com.ynsj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynsj.domain.SysRole;
import com.ynsj.mapper.SysRoleMapper;
import com.ynsj.service.SysRoleService;
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService{


    @Autowired
    private SysRoleMapper sysRoleMapper;
    /**
     * 判断一个用户是否是超级管理员
     * @param userId
     * @return
     */
    @Override
    public boolean isSuperAdmin(Long userId) {
        //当用户角色code包含ROLE_ADMIN 时，用户为超级管理员
        List<String> roleCodes = sysRoleMapper.getUserRoleCodes(userId);
        if (!roleCodes.isEmpty()&&roleCodes.contains("ROLE_ADMIN")){
            return true;
        }
        return false;
    }


    /**
     * 查询角色数据-》根据条件
     * @param sysRolePage
     * @param name
     * @return
     */
    @Override
    public Page<SysRole> findRolePage(Page<SysRole> sysRolePage, String name) {
        sysRolePage.addOrder(OrderItem.desc("last_update_time"));
        return page(sysRolePage,new LambdaQueryWrapper<SysRole>().like(
                !StringUtils.isEmpty(name), SysRole::getName, name
        ));
    }
}
